<template>
    <div class="address">
        <!--<van-nav-bar title="选择收货地址" left-arrow @click-left="goBack" />-->
        <!-- 地址列表内容 -->
        <div class="content">
            <van-address-list v-model="chosenAddressId" :list="list" @add="onAdd" @edit="onEdit" @select="onSelect"
                add-button-text="新增地址" />
        </div>
    </div>
</template>

<script>
    import Vue from 'vue'
    import { NavBar, Toast, AddressList,Dialog  } from 'vant';
    import Cookies from 'js-cookie';
    import { isDev } from "@/utils/tools";
    Vue.use(AddressList).use(NavBar).use(Dialog);

    export default {
        name: "recommendAddress",
        data() {
            return {
                activityId:'',
                chosenAddressId: '',
                list: [],
                recommendSchedule:{},
                operation:'',
                scheduleId:'',
            }
        },
        created() {
            if (null!=this.$route.query.activityId) {
                this.activityId = this.$route.query.activityId;
                this.recommendScheduleInfo();
            }

            if (null!=this.$route.query.operation) {

                  
                this.operation = this.$route.query.operation;
                
                if(!this.operation||this.operation.length<1){
                    return;
                }
                let pageTile = '';  
                if(this.operation=="select"){
                    pageTile = '选择收货地址';
                }else if(this.operation=="add"){
                    pageTile = '新增收货地址';
                }else if(this.operation=="edit"){
                    pageTile = '修改收货地址';
                }
                document.title = pageTile; 
            }

            if(null!=this.$route.query.scheduleId){
                this.scheduleId = this.$route.query.scheduleId;
            }
        },
        mounted(){
            this.getList();
        },
        methods: {
            recommendScheduleInfo() {
                this.token = Cookies.get("token")
                this.$api.personInf.recommendScheduleInfo({openId:this.token,activityId:this.activityId}).then(res=>{
                    if(res.code==0){
                        this.recommendSchedule = res.data;
                        if(this.recommendSchedule!=null&&this.recommendSchedule.userAddressId!=null&&this.recommendSchedule.userAddressId!=""){
                            this.chosenAddressId = this.recommendSchedule.userAddressId;
                        }

                    }else{
                        Toast(res.msg)
                    }
                }).catch(err=>{
                    console.log(err)
                })
            },
            getList() {
                this.token = Cookies.get("token")
                this.$api.personInf.userAddressList({openId:this.token}).then(res=>{
                    if(res.code==0){
                        this.list = res.list;
                    }else{
                        Toast(res.msg)
                    }
                }).catch(err=>{
                    console.log(err)
                })
            },
            onSelect(item,index){
                Dialog.confirm({
                title: '操作提示',
                message: '是否选择该地址为礼品收货地址'
                    }).then(() => {
                        // on confirm
                        this.token = Cookies.get("token")
                        this.$api.personInf.userAddressSelect({openId:this.token,activityId:this.activityId,userAddressId:item.id}).then(res=>{
                            if(res.code==0){
                                Toast("选择收货地址成功");
                                this.$router.go(-1);
                            }else{
                                Toast(res.msg)
                                this.chosenAddressId = this.recommendSchedule.userAddressId;
                            }
                        }).catch(err=>{
                            console.log(err)
                        })

                    }).catch(() => {
                    // on cancel
                    this.chosenAddressId = this.recommendSchedule.userAddressId;
                });
            },
            onAdd() {
                this.$router.push({
                    path: "/recommend_address_edit",
                    query: {
                        activityId:this.activityId,
                        scheduleId:this.scheduleId
                    }
                });
            },
            onEdit(item, index) {
                this.$router.push({
                    path: "/recommend_address_edit",
                    query: {
                        idx: index,
                        id: item.id,
                        activityId:this.activityId,
                        scheduleId:this.scheduleId
                    }
                });
            },
            // 回退
            goBack() {
                this.$router.push({
                    path: "/recommend_activity"
                });
            },
        }
    }
</script>

<style lang="less" scoped>
    .address {
    }
</style>
